﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace PcArduinoControl
{
	class Command : Packet
	{
		private static byte[] CombineTypeAndArgs(CommandType type, byte[] args)
		{
			byte[] returnValue = new byte[args.Length + 1];

			returnValue[0] = (byte)type;
			args.CopyTo(returnValue, 1);
			return returnValue;
		}

		public override byte StatedChecksum
		{
			get
			{
				return CalculatedChecksum;
			}
		}

		public CommandType Type
		{
			get
			{
				return (CommandType)Data.Skip(1).First();
			}
		}

		public byte[] Arguments
		{
			get
			{
				return Data.Skip(2).ToArray();
			}
		}

		/// <summary>
		/// Warning: This should only be used for intentionaly sending invalid commands and reciving invalid commands.
		/// </summary>
		/// <param name="type">Represents the desired command type.</param>
		internal Command(CommandType type) :
			this(type, new byte[0])
		{
		}

		/// <summary>
		/// Warning: This should only be used for intentionaly sending invalid commands and reciving invalid commands.
		/// </summary>
		/// <param name="type">Represents the desired command type.</param>
		/// <param name="args">Represents the desired command arguments.</param>
		internal Command(CommandType type, params byte[] args) :
			base(Direction.PcToArduino, CombineTypeAndArgs(type, args))
		{
		}
	}
}
